﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2012
// admin@franknagl.de
//
namespace SBIP.Helper
{
    using System;

    /// <summary>
    /// Helper class for common string operations, which are not provided by 
    /// default <see cref="String"/> class.
    /// </summary>
    public class StringBuilder
    {
        /// <summary>
        /// Splits the specified absolute file path in standalone file name and 
        /// directories.
        /// </summary>
        /// <param name="filePath">The file path to split.</param>
        /// <param name="file">The resulting standalone file name.</param>
        /// <param name="directories">The resulting array of the directories.</param>
        public static void SplitPathInFileAndDirectories(
            string filePath, out string file, out string[] directories)
        {
            char[] c = new [] {'/', '\\'};
            string[] tmpDirs = filePath.Split(c);
            file = tmpDirs.GetValue(tmpDirs.Length - 1) as string;
            directories = new string[tmpDirs.Length - 1];

            for (int i = 0; i < tmpDirs.Length - 1; i++)
            {
                directories[i] = tmpDirs[i];
            }
        }


        /// <summary>
        /// Gets the <b>first</b> substring delimited by the specified characters.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Is thrown, whether 
        /// <paramref name="startDelimiter"/> or <paramref name="endDelimiter"/> 
        /// is not part of <paramref name="text"/>.</exception>
        /// <param name="text">The text.</param>
        /// <param name="startDelimiter">The start delimiter.</param>
        /// <param name="endDelimiter">The end delimiter.</param>
        /// <param name="includingDelimiters">If set to <c>true</c> 
        /// the delimiters will be part of resulting substring.</param>
        /// <returns>The resulting substring.</returns>
        public static string Substring(
            string text, char startDelimiter, char endDelimiter, bool includingDelimiters = true)
        {
            const string errMsg = "Delimiter character is not part of string.";
            int del = includingDelimiters ? 1 : 0;
            int startIndex = text.IndexOf(startDelimiter) + 1 - del;
            int endIndex = text.IndexOf(endDelimiter) + del;
            int length = endIndex - startIndex;

            if (startIndex == -1)
            {
                throw new ArgumentOutOfRangeException("startDelimiter", errMsg);
            }

            if (endIndex == -1)
            {
                throw new ArgumentOutOfRangeException("endDelimiter", errMsg);
            }

            return text.Substring(startIndex, length);
        }

        /// <summary>
        /// Gets the <b>first</b> substring delimited by the specified characters.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Is thrown, whether 
        /// <paramref name="startDelimiter"/> or <paramref name="endDelimiter"/> 
        /// is not part of <paramref name="text"/>.</exception>
        /// <param name="text">The text.</param>
        /// <param name="startDelimiter">The start delimiter.</param>
        /// <param name="endDelimiter">The end delimiter.</param>
        /// <param name="includingDelimiters">If set to <c>true</c> 
        /// the delimiters will be part of resulting substring.</param>
        /// <returns>The resulting substring.</returns>
        public static string Substring(
            string text, string startDelimiter, string endDelimiter, bool includingDelimiters = true)
        {
            const string errMsg = "Delimiter string is not part of string.";
            int del1 = includingDelimiters ? startDelimiter.Length : 0;
            int del2 = includingDelimiters ? endDelimiter.Length : 0;
            int startIndex = text.IndexOf(startDelimiter) + startDelimiter.Length - del1;
            int endIndex = text.IndexOf(endDelimiter) + del2;
            int length = endIndex - startIndex;

            if (startIndex == -1)
            {
                throw new ArgumentOutOfRangeException("startDelimiter", errMsg);
            }

            if (endIndex == -1)
            {
                throw new ArgumentOutOfRangeException("endDelimiter", errMsg);
            }

            return text.Substring(startIndex, length);
        }
    }
}
